#include "SpriteGroup.h"

dkm::SpriteGroup::SpriteGroup(ID3DXSprite* dxSprite)
	:Component(true,true)
	,allSprites(std::vector<Sprite*>())
	,dxSprite(dxSprite)
{
	dkm::Engine::GetInstance()->AddComponent(this);
}

dkm::SpriteGroup::~SpriteGroup()
{
	dkm::Engine::GetInstance()->RemoveComponent(this);
}

void dkm::SpriteGroup::Update(const float dt)
{
}

void dkm::SpriteGroup::Draw()
{
	std::vector<Sprite*>::iterator iter = allSprites.begin();

	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
	
	while (iter != allSprites.end())
	{
		(*iter)->DrawBatched();

		++iter;
	}
	dxSprite->Flush();

	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
}

void dkm::SpriteGroup::AddSprite(Sprite* sprite)
{
	sprite->SetBatched(true);
	allSprites.push_back(sprite);
}

void dkm::SpriteGroup::RemoveSprite(Sprite* sprite)
{
	sprite->SetBatched(false);
	allSprites.erase(find(allSprites.begin(), allSprites.end(), sprite));
}

void dkm::SpriteGroup::MoveSprites(float x, float y)
{
	std::vector<Sprite*>::iterator iter = allSprites.begin();

	while (iter != allSprites.end())
	{
		(*iter)->Move(x,y);
		++iter;
	}
}

void dkm::SpriteGroup::ScaleSprites(float scaleX, float scaleY)
{
	std::vector<Sprite*>::iterator iter = allSprites.begin();

	while (iter != allSprites.end())
	{
		(*iter)->Scale(scaleX,scaleY);
		++iter;
	}
}
